package cn.iocoder.yudao.module.lfd.service.appraise;

import cn.iocoder.yudao.module.lfd.controller.admin.appointment.vo.AppointmentEnum;
import cn.iocoder.yudao.module.lfd.dal.dataobject.appointment.AppointmentDO;
import cn.iocoder.yudao.module.lfd.dal.dataobject.barber.BarberDO;
import cn.iocoder.yudao.module.lfd.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.lfd.dal.mysql.appointment.AppointmentMapper;
import cn.iocoder.yudao.module.lfd.dal.mysql.barber.BarberMapper;
import cn.iocoder.yudao.module.lfd.dal.mysql.customer.CustomerMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.util.*;
import cn.iocoder.yudao.module.lfd.controller.admin.appraise.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.appraise.AppraiseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.lfd.convert.appraise.AppraiseConvert;
import cn.iocoder.yudao.module.lfd.dal.mysql.appraise.AppraiseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 评价 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AppraiseService {

    @Resource
    private AppraiseMapper appraiseMapper;

    
    public Long createAppraise(AppraiseCreateReqVO createReqVO) {
        // 插入
        AppraiseDO appraise = AppraiseConvert.INSTANCE.convert(createReqVO);
        appraiseMapper.insert(appraise);
        // 返回
        return appraise.getId();
    }

    
    public void updateAppraise(AppraiseUpdateReqVO updateReqVO) {
        // 校验存在
        validateAppraiseExists(updateReqVO.getId());
        // 更新
        AppraiseDO updateObj = AppraiseConvert.INSTANCE.convert(updateReqVO);
        appraiseMapper.updateById(updateObj);
    }

    
    public void deleteAppraise(Long id) {
        // 校验存在
        validateAppraiseExists(id);
        // 删除
        appraiseMapper.deleteById(id);
    }

    private void validateAppraiseExists(Long id) {
        if (appraiseMapper.selectById(id) == null) {
//            throw exception(APPRAISE_NOT_EXISTS);
        }
    }

    
    public AppraiseDO getAppraise(Long id) {
        return appraiseMapper.selectById(id);
    }

    
    public List<AppraiseDO> getAppraiseList(Collection<Long> ids) {
        return appraiseMapper.selectBatchIds(ids);
    }

    
    public PageResult<AppraiseDO> getAppraisePage(AppraisePageReqVO pageReqVO) {
        return appraiseMapper.selectPage(pageReqVO);
    }

    
    public List<AppraiseDO> getAppraiseList(AppraiseExportReqVO exportReqVO) {
        return appraiseMapper.selectList(exportReqVO);
    }

    @Resource
    private AppointmentMapper appointmentMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private BarberMapper barberMapper;
    
    public void createMiniAppraise(MiniAppraiseCreateReqVO createReqVO) {
        AppointmentDO appointmentDO = appointmentMapper.selectById(createReqVO.getAppointmentId());
        //只能评价当天理发
        if (appointmentDO==null||appointmentDO.getStatus()!= AppointmentEnum.FINISH||!appointmentDO.getHairDate().isEqual(LocalDate.now())) {
            return;
        }
        CustomerDO customerDO = customerMapper.selectOne("openid", appointmentDO.getOpenid());
        if (customerDO==null) return;
        BarberDO barberDO = barberMapper.selectById(appointmentDO.getBarberId());
        if (barberDO==null) return;
        AppraiseDO.AppraiseDOBuilder appraiseDO = AppraiseDO.builder();
        appraiseDO.nickName( customerDO.getNickName() );
        appraiseDO.phone( customerDO.getPhone() );
        appraiseDO.label( createReqVO.getLabel() );
        appraiseDO.degree( createReqVO.getDegree() );
        appraiseDO.comment( createReqVO.getComment() );
        appraiseDO.barberName( barberDO.getNickName() );
        appraiseDO.avatar( customerDO.getAvatar() );
        appraiseDO.shopId( appointmentDO.getShopId() );
        appraiseDO.appointmentId( createReqVO.getAppointmentId() );
        appraiseMapper.insert(appraiseDO.build());
    }

}
